package com.steven.ourcontest.chat.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.steven.ourcontest.R
import com.steven.ourcontest.base.BaseViewBinder
import com.steven.ourcontest.chat.bean.ChatOnLineMessage
import com.steven.ourcontest.chat.bean.ChatReceiveMessage
import com.steven.ourcontest.chat.bean.ChatReceiveMessageBean
import com.steven.ourcontest.me.widget.GlideRoundTransform
import com.steven.ourcontest.net.NetConstants
import com.steven.ourcontest.utils.DateUtil

class ChatOnLineViewBinder(
    private val context: Context?,
    private val itemClickListener: (() -> Unit)
) : BaseViewBinder<ChatOnLineMessage, OnLineMsgViewHolder>() {
    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): OnLineMsgViewHolder {
        val mInflater = inflater.inflate(R.layout.item_online_message, parent, false)
        return OnLineMsgViewHolder(mInflater)
    }

    override fun onBindViewHolder(itemHolder: OnLineMsgViewHolder, itemData: ChatOnLineMessage) {
        itemHolder.apply {
            tv_time?.text = DateUtil.timeStamp2Date(itemData.createTime, null)
            tv_content?.text = itemData.postMessage
            if (context != null) {
                if (iv_icon != null) {
                    Glide.with(context)
                        .load(NetConstants.ICON_BASE_URL + itemData.sendUserIconUrl)
                        .error(R.drawable.ic_avatar)
                        .apply(
                            RequestOptions.bitmapTransform(
                                GlideRoundTransform(
                                    context,
                                    10
                                )
                            )
                        ).into(iv_icon)
                }
            }
            iv_tips?.isVisible = itemData.sendError
        }
    }
}

class OnLineMsgViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val iv_icon: ImageView? = itemView.findViewById(R.id.iv_icon)
    val tv_time: TextView? = itemView.findViewById(R.id.tv_time)
    val tv_content: TextView? = itemView.findViewById(R.id.tv_content)
    val iv_tips: ImageView? = itemView.findViewById(R.id.iv_tips)
}